package com.fzy.core.tags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

import com.fzy.core.entity.Page;
import com.fzy.core.utils.BooleanUtils;
import com.opensymphony.xwork.ActionContext;

public class ExtGridTag extends BodyTagSupport {

	private static final long serialVersionUID = -6705894077809162971L;
	String page;
	String showpager;
	List columns = new ArrayList();

	public int doStartTag() throws JspException {
		columns = new ArrayList();
		String basePath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		String html = "";
		html += "<div style=\"width: 100%;\"";
		html += "	class=\"x-panel x-grid-with-row-lines x-panel-default x-grid\">";
		html += "	<div";
		html += "		class=\"x-panel-header x-docked x-panel-header-default x-horizontal x-panel-header-horizontal x-panel-header-default-horizontal x-top x-panel-header-top x-panel-header-default-top x-docked-top x-panel-header-docked-top x-panel-header-default-docked-top x-unselectable\"";
		html += "		style=\"position: relative !important; width: 100%;\">";
		html += "		<div";
		html += "			class=\"x-panel-header-body x-panel-header-body-default x-panel-header-body-horizontal x-panel-header-body-default-horizontal x-panel-header-body-top x-panel-header-body-default-top x-panel-header-body-docked-top x-panel-header-body-default-docked-top x-panel-header-body-default-horizontal x-panel-header-body-default-top x-panel-header-body-default-docked-top x-box-layout-ct\"";
		html += "			style=\"width: 100%;\">";
		html += "			<div class=\"x-box-inner \" style=\"width: 100%; height: 17px;\">";
		html += "				<table cellpadding=\"0\" cellspace=\"0\" border=\"0\" width=\"100%\">";
		html += "					<tr>";
		html += "						<td>";
		html += "							<div";
		html += "								style=\"text-align: left; margin: 0pt; position: relative !important;\"";
		html += "								class=\"x-component x-panel-header-text-container x-box-item x-component-default\">";
		html += "								<span";
		html += "									class=\"x-panel-header-text x-panel-header-text-default\">查询结果</span>";
		html += "							</div>";
		html += "						</td>";
		html += "						<td width=\"15px\">";
		html += "							<div id=\"tool-1019\"";
		html += "								style=\"width: 15px; height: 15px; margin: 0pt; position: relative !important;\"";
		html += "								class=\"x-tool x-box-item x-tool-default\">";
		html += "								<img class=\"x-tool-collapse-top\"";
		html += "									src=\""+basePath+"/images/transparent.gif\">";
		html += "							</div>";
		html += "						</td>";
		html += "					</tr>";
		html += "				</table>";
		html += "			</div>";
		html += "		</div>";
		html += "	</div>";
		html += "	<div";
		html += "		class=\"x-panel-body x-grid-body x-panel-body-default x-panel-body-default x-layout-fit\"";
		html += "		style=\"width: 100%; position: relative; overflow: visible;\">";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		BodyContent bodycontent = getBodyContent();
		String html = bodycontent.getString();
		try {
			bodycontent.getEnclosingWriter().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doAfterBody();
	}

	public int doEndTag() throws JspException {
		Page page = (Page)ActionContext.getContext().getValueStack().findValue(this.page);
		
		if(page == null){
			page = new Page();
			page.setPageNo("1");
			page.setPageSize(Page.DEFAULT_PAGE_SIZE + "");
			page.setTotalCount(0);
		}
		
		String html = "";
		long count = page.getTotalCount();
		if(count < 0) count = 0;
		
		html += "<input type=\"hidden\" name=\"page.orderBy\" class=\"orderBy\" value=\""+StringUtils.trimToEmpty(page.getOrderBy())+"\" />";
		html += "<input type=\"hidden\" name=\"page.order\" class=\"order\" value=\""+StringUtils.trimToEmpty(page.getOrder())+"\" />";
		boolean booleanShowPager = BooleanUtils.toBoolean(showpager, true);
		if(booleanShowPager){
			html += "	<!-- pager -->";
			html += "	<div";
			html += "		class=\"x-toolbar x-docked x-toolbar-default x-docked-bottom x-toolbar-docked-bottom x-toolbar-default-docked-bottom x-box-layout-ct\"";
			html += "		style=\"position: relative !important; width: 100%;\">";
			html += "		<div class=\"x-box-inner \"";
			html += "			style=\"position: relative !important; width: 100%; height: 22px;\">";
			html += "			<table style=\"width: 100%\" cellspace=\"0\" cellpadding=\"0\"";
			html += "				border=\"0\">";
			html += "				<tr>";
			html += "					<td align=\"left\">";
			html += "						<table cellspace=\"0\" cellpadding=\"0\" border=\"0\">";
			html += "							<tr>";
			html += "								<td>";
			html += "									<div";
			html += "										class=\"x-toolbar-text x-box-item x-toolbar-item x-toolbar-text-default\"";
			html += "										style=\"position: relative !important; margin: 0pt; font-size: 12px;\">第</div>";
			html += "								</td>";
			html += "								<td>";
			html += "									<table cellpadding=\"0\"";
			html += "										style=\"width: 30px; margin: 0pt; position: relative !important;\"";
			html += "										class=\"x-field x-tbar-page-number x-form-item x-box-item x-toolbar-item x-field-default-toolbar x-hbox-form-item x-form-dirty\">";
			html += "										<tbody>";
			html += "											<tr>";
			html += "												<td colspan=\"3\" class=\"x-form-item-body \"";
			html += "													style=\"width: 100%;\"><table cellspacing=\"0\"";
			html += "														cellpadding=\"0\"";
			html += "														class=\"x-form-trigger-wrap x-unselectable\"";
			html += "														style=\"-moz-user-select: -moz-none; width: 100%; table-layout: fixed;\">";
			html += "														<tbody>";
			html += "															<tr>";
			html += "																<td class=\"x-form-trigger-input-cell\"";
			html += "																	style=\"width: 100%;\"><input type=\"text\"";
			html += "																	class=\"x-form-field x-form-text pageNo\"";
			html += "																	style=\"width: 100%;\"";
			html += "																	value=\""+page.getPageNo()+"\" name=\"page.pageNo\"";
			html += "																	>";
			html += "																</td>";
			html += "															</tr>";
			html += "														</tbody>";
			html += "													</table>";
			html += "												</td>";
			html += "											</tr>";
			html += "										</tbody>";
			html += "									</table>";
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										class=\"x-toolbar-text x-box-item x-toolbar-item x-toolbar-text-default\"";
			html += "										style=\"position: relative !important; margin: 0pt; font-size: 12px;\">页&nbsp;共&nbsp;"+page.getTotalPages()+"&nbsp;页,&nbsp;每页显示</div>";
			html += "								</td>";
			html += "								<td>";
			html += "									<table cellpadding=\"0\"";
			html += "										style=\"width: 30px; margin: 0pt; position: relative !important;\"";
			html += "										class=\"x-field x-tbar-page-number x-form-item x-box-item x-toolbar-item x-field-default-toolbar x-hbox-form-item x-form-dirty\">";
			html += "										<tbody>";
			html += "											<tr>";
			html += "												<td colspan=\"3\" class=\"x-form-item-body \"";
			html += "													style=\"width: 100%;\"><table cellspacing=\"0\"";
			html += "														cellpadding=\"0\"";
			html += "														class=\"x-form-trigger-wrap x-unselectable\"";
			html += "														style=\"-moz-user-select: -moz-none; width: 100%; table-layout: fixed;\">";
			html += "														<tbody>";
			html += "															<tr>";
			html += "																<td class=\"x-form-trigger-input-cell\"";
			html += "																	style=\"width: 100%;\"><input type=\"text\"";
			html += "																	class=\"x-form-field x-form-text pageSize\"";
			html += "																	style=\"width: 100%;\"";
			html += "																	value=\""+page.getPageSize()+"\" name=\"page.pageSize\"";
			html += "																	>";
			html += "																</td>";
			html += "															</tr>";
			html += "														</tbody>";
			html += "													</table>";
			html += "												</td>";
			html += "											</tr>";
			html += "										</tbody>";
			html += "									</table>";
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										class=\"x-toolbar-text x-box-item x-toolbar-item x-toolbar-text-default\"";
			html += "										style=\"position: relative !important; margin: 0pt; font-size: 12px;\">条记录&nbsp;共&nbsp;"+count+"&nbsp;条记录&nbsp;</div>";
			html += "								</td>";
			html += "							</tr>";
			html += "						</table></td>";
			html += "					<td align=\"right\">";
			html += "						<table cellspace=\"0\" cellpadding=\"0\" border=\"0\">";
			html += "							<tr>";
			html += "								<td>";
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-btn x-box-item x-toolbar-item x-btn-default-toolbar-small x-icon x-btn-icon x-btn-default-toolbar-small-icon"+(page.isHasPre() ? "" : " x-item-disabled x-disabled x-btn-disabled x-btn-default-toolbar-small-disabled")+"\">";
			html += "										<em><button" + (page.isHasPre() ? " onclick=\"javascript:jumpPage(1,this)\"" : "");
			html += "												hidefocus=\"true\" class=\"x-btn-center\" type=\"button\"";
			html += "												title=\"首页\" style=\"height: 16px;\">";
			html += "												<span style=\"\" class=\"x-btn-inner\">&nbsp;</span><span";
			html += "													class=\"x-btn-icon x-tbar-page-first\"></span>";
			html += "											</button> </em>";
			html += "									</div></td>";
			html += "								<td>";
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-btn x-box-item x-toolbar-item x-btn-default-toolbar-small x-icon x-btn-icon x-btn-default-toolbar-small-icon"+(page.isHasPre() ? "" : " x-item-disabled x-disabled x-btn-disabled x-btn-default-toolbar-small-disabled")+"\">";
			html += "										<em><button" + (page.isHasPre() ? " onclick=\"javascript:jumpPage("+page.getPrePage()+",this)\"" : "");
			html += "												hidefocus=\"true\" class=\"x-btn-center\"";
			html += "												type=\"button\" title=\"上一页\"";
			html += "												style=\"height: 16px;\">";
			html += "												<span style=\"\" class=\"x-btn-inner\">&nbsp;</span><span";
			html += "													class=\"x-btn-icon x-tbar-page-prev\"></span>";
			html += "											</button> </em>";
			html += "									</div></td>";
			html += "								<td>";
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-btn x-box-item x-toolbar-item x-btn-default-toolbar-small x-icon x-btn-icon x-btn-default-toolbar-small-icon"+(page.isHasNext() ? "" : " x-item-disabled x-disabled x-btn-disabled x-btn-default-toolbar-small-disabled")+"\">";
			html += "										<em><button" + (page.isHasNext() ? " onclick=\"javascript:jumpPage("+page.getNextPage()+",this)\"" : "");
			html += "												hidefocus=\"true\" class=\"x-btn-center\" type=\"button\"";
			html += "												title=\"下一页\" style=\"height: 16px;\">";
			html += "												<span style=\"\" class=\"x-btn-inner\">&nbsp;</span><span";
			html += "													class=\"x-btn-icon x-tbar-page-next\"></span>";
			html += "											</button> </em>";
			html += "									</div>";
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-btn x-box-item x-toolbar-item x-btn-default-toolbar-small x-icon x-btn-icon x-btn-default-toolbar-small-icon"+(page.isHasNext() ? "" : " x-item-disabled x-disabled x-btn-disabled x-btn-default-toolbar-small-disabled")+"\">";
			html += "										<em><button" + (page.isHasNext() ? " onclick=\"javascript:jumpPage("+page.getTotalPages()+",this)\"" : "");
			html += "												hidefocus=\"true\" class=\"x-btn-center\" type=\"button\"";
			html += "												title=\"末页\" style=\"height: 16px;\">";
			html += "												<span style=\"\" class=\"x-btn-inner\">&nbsp;</span><span";
			html += "													class=\"x-btn-icon x-tbar-page-last\"></span>";
			html += "											</button> </em>";
			html += "									</div>";
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-toolbar-separator x-box-item x-toolbar-item x-toolbar-separator-horizontal\"></div>";
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-btn x-box-item x-toolbar-item x-btn-default-toolbar-small x-icon x-btn-icon x-btn-default-toolbar-small-icon\">";
			html += "										<em><button" + " onclick=\"javascript:jumpPage("+page.getPageNo()+",this)\"";
			html += "												hidefocus=\"true\" class=\"x-btn-center\" type=\"button\"";
			html += "												title=\"刷新\" style=\"height: 16px;\">";
			html += "												<span style=\"\" class=\"x-btn-inner\">&nbsp;</span><span";
			html += "													class=\"x-btn-icon x-tbar-loading\"></span>";
			html += "											</button> </em>";
			html += "									</div>";
			html += "								</td>";
			html += "							</tr>";
			html += "						</table></td>";
			html += "				</tr>";
			html += "			</table>";
			html += "		</div>";
			html += "	</div>";
		}
		html += "</div>";
		html += "</div>";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public List getColumns() {
		return columns;
	}

	public void setColumns(List columns) {
		this.columns = columns;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getShowpager() {
		return showpager;
	}

	public void setShowpager(String showpager) {
		this.showpager = showpager;
	}
	
}
